package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;


    //通过传入中间实体类对象来添加菜品
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //创建套餐对象保存套餐基本信息
        //传入参数对基本信息复制
        this.save(setmealDto);
        //剩下的信息保存到setmeal-dish中间表
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes.stream().map(dish -> {
            dish.setSetmealId(setmealDto.getId());
            return dish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }

    //通过传入id来回显要修改的套餐
    @Override
    public R<SetmealDto> get(long id) {
        //创建返回的对象
        SetmealDto setmealDto = new SetmealDto();
        //基础属性赋值
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        //口味赋值
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        //菜品分类的名称赋值,此处已通过另外一个接口实现，重复实现会导致循环调用
//        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
//        categoryWrapper.eq(Category::getId, setmeal.getCategoryId());
//        setmealDto.setCategoryName(categoryService.getOne(categoryWrapper).getName());
        return R.success(setmealDto);
    }

    //通过传入的实体数据修改两张表信息
    @Override
    public R<String> update(SetmealDto setmealDto) {
        //查看此套餐的状态，停售则删。
        if (setmealDto.getStatus() == 1) {
            return R.error("莹莹温馨提示：请先停售再修改哦！！");
        }
        //根据传入的数据查到套餐表并删除
        this.removeById(setmealDto.getId());
        //找到中间表的记录删除
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishWrapper);
        //添加中间表
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
        //添加基本信息
        this.save(setmealDto);
        return R.success("莹莹温馨提示：删除成功！！！");

    }

    //通过传入id修改套餐状态
    @Override
    public R<String> changeStatus(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Setmeal::getId, ids).set(Setmeal::getStatus, status);

        this.update(wrapper);
        return R.success("修改成功！！");
    }

    //通过id来实现批量删除或删除
    @Override
    public R<String> removeByIdsMy(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = this.getById(id);
            Integer status = setmeal.getStatus();
            if (status == 1) {
                return R.error("莹莹温馨提示：套餐正在售卖，不可以删除");
            }
            this.removeById(id);
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            setmealDishService.remove(wrapper);
        }
        return R.success("删除成功");
    }
}
